<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 多维数组</title>
    <link rel="stylesheet" href="common.css">
    <style>
        /* 页面特有样式 */
        .array-card {
            background-color: #f5f9ff;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 25px;
            border-left: 4px solid #5D9CEC;
        }
        
        .definition-box {
            background-color: #f0f7ff;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .matrix-box {
            background-color: #fff3e0;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .visualization {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
            font-family: monospace;
        }
        
        .warning-note {
            background-color: #ffebee;
            border-left: 4px solid #f44336;
            padding: 12px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言多维数组</h2>
            
            <section>
                <h3>多维数组概述</h3>
                <p>多维数组是数组的数组，最常用的是二维数组(矩阵)。</p>
            </section>
            
            <div class="array-card">
                <h3>1. 二维数组定义</h3>
                
                <div class="definition-box">
                    <h4>声明语法</h4>
                    <pre><code>数据类型 数组名[行数][列数];</code></pre>
                    
                    <h4>初始化方式</h4>
                    <pre><code>// 完全初始化
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// 部分初始化(剩余元素为0)
int matrix[3][4] = {
    {1},
    {0, 2},
    {0, 0, 3}
};

// 连续初始化(按内存顺序)
int matrix[2][3] = {1, 2, 3, 4, 5, 6};

// C99指定元素初始化
int matrix[3][3] = {
    [0][0] = 1,
    [1][1] = 1,
    [2][2] = 1
};</code></pre>
                </div>
                
                <div class="visualization">
                    <h4>内存布局</h4>
                    <p>二维数组在内存中是按行连续存储的：</p>
                    <pre>
matrix[0][0] | matrix[0][1] | matrix[0][2] |
matrix[1][0] | matrix[1][1] | matrix[1][2] |
...
                    </pre>
                </div>
                
                <div class="warning-note">
                    <h4>注意事项</h4>
                    <ul>
                        <li>行数可以省略，列数不能省略：<code>int arr[][3] = {...};</code></li>
                        <li>实际是数组的数组，不是真正的矩阵</li>
                        <li>访问越界是未定义行为</li>
                    </ul>
                </div>
            </div>
            
            <div class="array-card">
                <h3>2. 矩阵运算</h3>
                
                <div class="matrix-box">
                    <h4>矩阵转置</h4>
                    <pre><code>void transpose(int rows, int cols, int src[rows][cols], int dst[cols][rows]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dst[j][i] = src[i][j];
        }
    }
}</code></pre>
                </div>
                
                <div class="matrix-box">
                    <h4>矩阵乘法</h4>
                    <pre><code>void matrixMultiply(int m, int n, int p, 
                   int A[m][n], int B[n][p], int C[m][p]) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < p; j++) {
            C[i][j] = 0;
            for (int k = 0; k < n; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}</code></pre>
                </div>
                
                <div class="matrix-box">
                    <h4>矩阵加法</h4>
                    <pre><code>void matrixAdd(int rows, int cols, 
               int A[rows][cols], int B[rows][cols], 
               int C[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
}</code></pre>
                </div>
                
                <div class="warning-note">
                    <h4>性能提示</h4>
                    <ul>
                        <li>按行访问比按列访问效率高(缓存友好)</li>
                        <li>小矩阵适合直接使用二维数组</li>
                        <li>大矩阵考虑使用一维数组模拟</li>
                    </ul>
                </div>
            </div>
            
            <div class="array-card">
                <h3>3. 三维数组简介</h3>
                
                <div class="definition-box">
                    <h4>声明与初始化</h4>
                    <pre><code>// 声明三维数组
int cube[2][3][4];  // 2层，每层3行4列

// 初始化
int cube[2][3][4] = {
    { // 第一层
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    },
    { // 第二层
        {13, 14, 15, 16},
        {17, 18, 19, 20},
        {21, 22, 23, 24}
    }
};</code></pre>
                </div>
                
                <div class="visualization">
                    <h4>三维数组遍历</h4>
                    <pre><code>for (int i = 0; i < 2; i++) {        // 层
    for (int j = 0; j < 3; j++) {     // 行
        for (int k = 0; k < 4; k++) { // 列
            printf("%d ", cube[i][j][k]);
        }
        printf("\n");
    }
    printf("\n");
}</code></pre>
                </div>
                
                <div class="warning-note">
                    <h4>使用建议</h4>
                    <ul>
                        <li>三维数组内存消耗大，谨慎使用</li>
                        <li>考虑使用结构体或一维数组替代</li>
                        <li>高维数组可读性较差</li>
                    </ul>
                </div>
            </div>
            
            <section>
                <h3>综合示例：矩阵运算</h3>
                <pre><code>#include &lt;stdio.h&gt;

#define ROWS 3
#define COLS 3

void printMatrix(int rows, int cols, int mat[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%3d", mat[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int A[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    int B[ROWS][COLS] = {
        {9, 8, 7},
        {6, 5, 4},
        {3, 2, 1}
    };
    
    int C[ROWS][COLS];
    
    printf("矩阵A:\n");
    printMatrix(ROWS, COLS, A);
    
    printf("\n矩阵B:\n");
    printMatrix(ROWS, COLS, B);
    
    // 矩阵相加
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
    
    printf("\n矩阵A+B:\n");
    printMatrix(ROWS, COLS, C);
    
    return 0;
}</code></pre>
            </section>
        </main>
        
        <footer>
             <p>&copy; </p>
        </footer>
        
        <!-- 二维码区域 -->

    </div> <!-- 结束.container -->
</body>
</html>